import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Modal, notification, Tabs} from "antd";
import {create, hideCreate, query} from "./actions";
import {getCurrentOrganization, getPrincipal} from "../../lib/identity";
import OrderInfoModal from "./OrderInfoModal";
import {formatDate, formatDateTime} from "../../lib/func";
import OrderFeeModal from "./OrderFeeModal";
import OrderOutFeeModal from "./OrderOutFeeModal";

const TabPane = Tabs.TabPane;

class CreateModal extends PureComponent {
    handleCreate = () => {
        const {dispatch, model, fees, outFees, follows, page, pageSize, homeFee, sheetFee, homeCount, sheetCount, filter} = this.props;
        if (model.organizationId === undefined || model.organizationId === null || model.organizationId.length === 0) {
            model.organizationId = getCurrentOrganization();
        }
        if (model.clientId === undefined || model.clientId === null || model.clientId.length === 0) {
            notification.error({
                message: '创建失败:客户必填'
            });
            return;
        }
        model.pickDate = formatDate(model.pickDate);
        if (follows[0] && follows[0].remindDate) {
            follows[0].remindDate = formatDate(follows[0].remindDate);
        }
        if (follows[0] && follows[0].arriveTime) {
            follows[0].arriveTime = formatDateTime(follows[0].arriveTime);
        }
        model.preDepartureTime = formatDate(model.preDepartureTime);
        model.actDepartureTime = formatDate(model.actDepartureTime);
        model.packageReturnDate = formatDate(model.packageReturnDate);
        model.orderArriveTime = formatDateTime(model.orderArriveTime);
        model.orderLeaveTime = formatDateTime(model.orderLeaveTime);
        if (getPrincipal().admin === false) {
            model.organizationId = getCurrentOrganization();
        }
        model.homeFee = homeFee;
        model.sheetFee = sheetFee;
        model.homeCount = homeCount;
        model.sheetCount = sheetCount;
        // 检查应付费用大于0并且没有选择承运商的
        const found2 = outFees.find(item => item.feeCount > 0 && (item.currency === undefined || item.currency === null || item.currency.length === 0));
        if (found2) {
            notification.error({
                message: "应付金额有值，但没有选择币种"
            });
            return;
        }
        const found3 = outFees.find(item => item.feeCount > 0 && (item.carrierId === undefined || item.carrierId === null || item.carrierId.length === 0));
        if (found3) {
            notification.error({
                message: "应付金额有值，但没有选择供应商"
            });
            return;
        }
        dispatch(create({
            ...model, "opId": getPrincipal().id,
            "feeList": fees,
            "outFeeList": outFees,
            "followDtos": follows
        })).then(action => {
            if (action.error !== true) {
                dispatch(hideCreate());
                dispatch(query({...filter, "userId": getPrincipal().id, "organizationId": getCurrentOrganization(), page, pageSize}));
                notification.success({
                    message: '创建成功'
                });
            }
        });
    };
    handleCancel = () => {
        const {dispatch} = this.props;
        dispatch(hideCreate());
    };

    changeTab = activeKey => {
        if (activeKey !== "1") {
            /*const {dispatch, model} = this.props;
            dispatch(calculateFee(model));*/
        }
    };

    render() {
        const {loading, visible, model} = this.props;
        return (
            <Modal
                title="新增"
                visible={visible}
                onOk={this.handleCreate}
                onCancel={this.handleCancel}
                confirmLoading={loading}
                okText="保存"
                cancelText="取消"
                width="80%"
            >
                <Tabs defaultActiveKey="1" onChange={this.changeTab}>
                    <TabPane tab="订单信息" key="1">
                        <OrderInfoModal model={model}/>
                    </TabPane>
                    {/*<TabPane tab="订单跟进" key="2">
                        <OrderFollowModal model={model}/>
                    </TabPane>*/}
                    <TabPane tab="应收录入" key="3">
                        <OrderFeeModal model={model}/>
                    </TabPane>
                    <TabPane tab="应付录入" key="4">
                        <OrderOutFeeModal model={model}/>
                    </TabPane>
                </Tabs>
            </Modal>

        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.zadOrder.create,
        fees: state.zadOrder.orderFee.feeList,
        homeFee: state.zadOrder.orderFee.homeFee,
        sheetFee: state.zadOrder.orderFee.sheetFee,
        homeCount: state.zadOrder.orderFee.homeCount,
        sheetCount: state.zadOrder.orderFee.sheetCount,
        outFees: state.zadOrder.orderOutFee.feeList,
        follows: state.zadOrder.orderFollows.follows,
        clientList: state.zadOrder.list.clientList,
        carrierList: state.zadOrder.list.carrierList,
        filter: state.zadOrder.list.filter
    };
};

export default connect(mapStateToProps)(CreateModal);
